/*
 * Created on Nov 13, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.mtdownload.download;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Vector;


/**
 * The manager class that handles all the downloads. 
 * It takes care of all the listeners
 * @author sergiu
 */
public class DownloadManager implements Serializable{
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 3256728394065392949L;
	
	private static Vector<DownloadTask> tasks=new Vector<DownloadTask>();
	private static Vector<DownloadTaskPropertiesChanged> taskPropertiesChangedEvents=new Vector<DownloadTaskPropertiesChanged>();
	private static Vector<DownloadMasterThread> masterThreads=new Vector<DownloadMasterThread>();
	private static DownloadManager instance=null;
	
	public static void addDownloadTaskPropertiesChangedListener(DownloadTaskPropertiesChanged listener){
		taskPropertiesChangedEvents.add(listener);		
	}
	public static void removeDownloadTaskPropertiesChangedListener(DownloadTaskPropertiesChanged listener){
		taskPropertiesChangedEvents.remove(listener);
	}
	private static void notifyDownloadTaskPropertiesChangedListeners(final DownloadTask task){
		for(DownloadTaskPropertiesChanged listener:taskPropertiesChangedEvents){
			listener.propertiesChanged(task);
		}
	}
	public static void addDownloadTask(DownloadTask task){
		if(task==null)return;		
		tasks.add(task);
		task.setIndex(tasks.size()-1);
		if(task.getStartTime()==DownloadTask.START_NOW){
			startDownload(task);
		}
	}
	
	public static DownloadTask getDownloadTaskAt(int index){
		return tasks.get(index);
	}
	
	public static int getDownloadTasksListSize(){
		return tasks.size();
	}
	public static void removeDownloadTask(DownloadTask task){
		tasks.remove(task);
	}
	
	public static void stopAllDownloads(){
		for(DownloadTask task:tasks){
			if(!task.isComplete()){
				task.setStatus("Paused");
				task.setStarted(false);
			}
		}
	}
	
	/**
	 * Private empty constructor
	 */
	private DownloadManager(){
		//empty		
	}
	
	private static DownloadManager getInstance(){
		if(instance==null){
			instance=new DownloadManager(); 
		}
		return instance;
	}
	
	public void notifyListeners(DownloadTask task){
		notifyDownloadTaskPropertiesChangedListeners(task);
	}
	
	public synchronized static void startDownload(final DownloadTask task){
		if(task.isStarted() || task.isComplete()) return;
		System.out.println("starting (dldmanager):"+task);
		DownloadMasterThread thread=null;
		if(task.getMasterThread()==null){
			thread=new DownloadMasterThread(task);
			thread.setManager(getInstance());
			masterThreads.add(thread);
			task.setMasterThread(thread);
		}else{
			thread=task.getMasterThread();
		}
		new Thread(thread).start();
	}	
	
	public static void saveDownloadList(String file){
		try {
			File f=new File(file);
			f.getParentFile().mkdirs();
			
			FileOutputStream out=new FileOutputStream(file);
			ObjectOutputStream objOut=new ObjectOutputStream(out);
			objOut.writeObject(tasks);
			objOut.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void loadDownloadList(String file){
		try {
			File f=new File(file);
			if(f.exists()){
				FileInputStream in=new FileInputStream (file);
				ObjectInputStream objIn=new ObjectInputStream(in);
				Object objTasks=objIn.readObject();
				if(objTasks instanceof Vector){
					tasks=(Vector<DownloadTask>)objTasks;					
				}
				objIn.close();
			}						
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
}
